package com.etlmaster.agent;

import java.io.IOException;

import com.etlmaster.executor.utils.DataCollectionUtils;
import com.etlmaster.executor.utils.LogWriter;
import com.etlmaster.executor.utils.ProcessCallback;
import com.etlmaster.executor.utils.ProcessHelper;

/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
public class Task {
	public static String runTask(String json) {
		
		String cmdPath = DataCollectionUtils.getProperties("runLocalCmdPath");
		String rtnJson="";
		try{
			
			LogWriter.addLog("INFO","运行命令{} {}",cmdPath,json);
			ProcessCallback processCallback = new ProcessCallback(){
				@Override
				public void handle(String lineStr) {
					LogWriter.addLog("INFO","[pid {}]:{}",getProcessId(),lineStr);
				}
				@Override
				public void finish() {
					LogWriter.addLog("INFO","退出进程[pid {}]",getProcessId());
				}
			};
			LogWriter.addLog("INFO","启动进程[pid {}]",processCallback.getProcessId());
			int rtnCode=ProcessHelper.executor(processCallback, cmdPath,json);
			
			if(rtnCode==0){
				rtnJson="{\"param\":\""+json+"\",\"rtnCode\":"+rtnCode+",\"rtnMsg\":\"success\"}";
			}else{
				rtnJson="{\"param\":\""+json+"\",\"rtnCode\":"+rtnCode+",\"rtnMsg\":\"return code is:"+rtnCode+"\"}";

			}
		} catch (IOException e) {
			e.printStackTrace();
			rtnJson="{\"param\":\""+json+"\",\"rtnCode\":"+-1+",\"rtnMsg\":\""+e.getMessage()+"\"}";

		}
		return rtnJson;
		
	}

}
